//
//  BGBuyerListDetaiCell.m
//  bigMall
//
//  Created by LTOVE on 16/9/10.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGBuyerListDetaiCell.h"
#import "BGBuyerListDetailCellModel.h"

#import "BGMessageImageView.h"
#import "BGMessageListLayout.h"

static NSString *const reuseIdentify = @"cell";
@implementation BGBuyerListDetaiCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the view for the selected state
}

- (void)setModel:(BGBuyerListDetailCellModel *)model
{
    _model = model;
    [self setUpAllSubviewsWithModel:model];
}

/**
 *  根据模型添加相对应的控件视图
 *
 *  @param model model description
 */
- (void)setUpAllSubviewsWithModel:(BGBuyerListDetailCellModel *)model
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIView *contentView = [self toInitContentViewWithHeigh:model.height];
    
//    contentView.backgroundColor = [UIColor redColor];
//    self.contentView.backgroundColor = [UIColor clearColor];
    
    if ([model isKindOfClass:[BGBuyerListDetailCellNumModel class]]) {
         //订单编号
        BGBuyerListDetailCellNumModel *itemModel = (BGBuyerListDetailCellNumModel *)model;
        
        NSString *listNum = [NSString stringWithFormat:@"订单编号:%@",itemModel.listNumStr];
        UILabel *listNumLabel = [UILabel new];
        NSMutableAttributedString *attrStr = [self attrStrWithStr:listNum withColor:textGrayColor];
        listNumLabel.attributedText = attrStr;
        [listNumLabel sizeToFit];
        listNumLabel.x = BGMaginLeft;
        listNumLabel.centerY = itemModel.height * 0.5;
        [contentView addSubview:listNumLabel];
        UILabel *listNumDetailLabel = [UILabel new];
        listNumDetailLabel.text = itemModel.listNumSecStr;
        listNumDetailLabel.font = Font30;
        listNumDetailLabel.textColor = read_;
        [listNumDetailLabel sizeToFit];
        listNumDetailLabel.centerY = listNumLabel.centerY;
        listNumDetailLabel.x = screenW - listNumDetailLabel.width - BGMaginLeft;
        [contentView addSubview:listNumDetailLabel];
        
    }else if ([model isKindOfClass:[BGBuyerListDetailCellUserInfoModel class]]){
        // 用户信息
        BGBuyerListDetailCellUserInfoModel *itemModel = (BGBuyerListDetailCellUserInfoModel *)model;
        contentView.backgroundColor = dockwhite_;
//        NSString *userNameStr = [NSString stringWithFormat:@"用户名:%@",itemModel.userName];
//        NSString *userPhoneStr = [NSString stringWithFormat:@"联系电话:%@",itemModel.userPhoneNum];
//        NSString *comStr = [NSString stringWithFormat:@"公司名称:%@",itemModel.comName];
        
        UIView *listView = [BGMessageListLayout initWithFrame:contentView.frame andDataArray:@[[self getDictWithTitle:@"用户名" andValue:itemModel.userName],[self getDictWithTitle:@"联系电话" andValue:itemModel.userPhoneNum],[self getDictWithTitle:@"公司名称" andValue:itemModel.comName]] andIsDouble:YES];
        listView.backgroundColor = [UIColor clearColor];
        [contentView addSubview:listView];
        //发送站内信按钮
        UIButton *sendMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [sendMessageBtn setTitle:@"发送站内信" forState:UIControlStateNormal];
        sendMessageBtn.font = Font28;
        [sendMessageBtn sizeToFit];
        sendMessageBtn.height = 15;
        sendMessageBtn.width = sendMessageBtn.width + 10;
//        sendMessageBtn.height = 15;
        [sendMessageBtn setBackgroundImage:[UIImage imageWithColor:read_ size:sendMessageBtn.size] forState:UIControlStateNormal];
        sendMessageBtn.x = screenW *0.5 + BGMaginLeft;
        sendMessageBtn.y = contentView.height - BGMaginButtom - sendMessageBtn.height;
        sendMessageBtn.layer.cornerRadius = 5;
        sendMessageBtn.layer.masksToBounds = YES;
        [listView addSubview:sendMessageBtn];
        [sendMessageBtn addTarget:self action:@selector(sendMessageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }else if ([model isKindOfClass:[BGBuyerListDetailCellTitleModel class]]){
        //标题
        BGBuyerListDetailCellTitleModel *itemModel = (BGBuyerListDetailCellTitleModel *)model;
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = itemModel.titleStr;
        titleLabel.font = Font34;
        titleLabel.textColor = textBlackColor;
        [titleLabel sizeToFit];
        titleLabel.x = BGMaginLeft;
        titleLabel.centerY = contentView.height * 0.5;
        [contentView addSubview:titleLabel];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(BGMaginLeft, contentView.height - 1, screenW - 2 * BGMaginLeft, 1)];
        lineView.backgroundColor = dockwhite_;
        [contentView addSubview:lineView];
    }else if ([model isKindOfClass:[BGBuyerListDetailCellDynamicDataModel class]]){
        //动态数据部分
        BGBuyerListDetailCellDynamicDataModel *itemModel = (BGBuyerListDetailCellDynamicDataModel *)model;
        UIView *listView = [BGMessageListLayout initWithFrame:contentView.frame andDataArray:itemModel.dynamicDataArray andIsDouble:YES];
        [contentView addSubview:listView];
    }else if ([model isKindOfClass:[BGBuyerListDetailCellCDPicModel class]]){
        //仓单图片
        BGBuyerListDetailCellCDPicModel *itemModel = (BGBuyerListDetailCellCDPicModel *)model;
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = [NSString stringWithFormat:@"%@:",itemModel.title];
        titleLabel.textColor = textBlackColor;
        titleLabel.font = Font30;
        [titleLabel sizeToFit];
        titleLabel.x = BGMaginLeft;
        titleLabel.y = 5;
        [contentView addSubview:titleLabel];
        UIView *imageView = [BGMessageImageView initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame) + 5, screenW, contentView.height - 10 - titleLabel.height) andDataArray:itemModel.imageDataArray];
        [contentView addSubview:imageView];
    }else if ([model isKindOfClass:[BGBuyerListDetailCellPicModel class]]){
        //报告部分图片
        BGBuyerListDetailCellPicModel *itemModel = (BGBuyerListDetailCellPicModel *)model;
        UIView *imageView = [BGMessageImageView initWithFrame:contentView.frame andDataArray:itemModel.imageDataArray];
        [contentView addSubview:imageView];
    }else if ([model isKindOfClass:[BGBuyerListDetailCellLineModel class]]){
        contentView.backgroundColor = dockwhite_;
    }
    [self.contentView addSubview:contentView];

    
}

- (NSDictionary *)getDictWithTitle:(NSString *)title andValue:(NSString *)value
{
    if (!value) {
        value = @"";
    }
    return @{
             @"title":title,
             @"value":value
             };
    //    return [NSString stringWithFormat:@"%@:%@",title,value];
}

/**
 *  发送站内信按钮点击
 */
- (void)sendMessageBtnClick
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"buyerListDetail" object:nil];
}

- (NSMutableAttributedString *)attrStrWithStr:(NSString *)str withColor:(UIColor *)color
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:textBlackColor
                          };
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:str attributes:dic];
    NSRange rang = [str rangeOfString:@":"];
    NSDictionary *secDic = @{
                             NSFontAttributeName:Font30,
                             NSForegroundColorAttributeName:color
                             };
    [attr addAttributes:secDic range:NSMakeRange(rang.location + 1, str.length -1 - rang.location)];
    return attr;
}
- (UIView *)toInitContentViewWithHeigh:(CGFloat)heigh;
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, heigh)];
    return view;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    BGBuyerListDetaiCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
        
    }
    return cell;
}

@end
